package com.zshan.clinic.web.controller;


import com.zshan.clinic.common.entity.HeaderDTO;
import com.zshan.clinic.common.response.RestResp;
import com.zshan.clinic.web.entity.CaptchaImageVo;
import com.zshan.clinic.web.entity.UserInfo;
import com.zshan.clinic.web.entity.request.AccountLoginRequest;
import com.zshan.clinic.web.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


/**
 * 登录
 */
@RestController
@RequestMapping("/login")
public class LoginController extends AbstractController{


    @Autowired
    private LoginService loginService;

    /**
     * 生成图形验证码
     * @param
     * @return
     */
    @RequestMapping(value = "/captchaImage", method = RequestMethod.POST)
    public RestResp<CaptchaImageVo> captchaImage() {
        CaptchaImageVo captchaImageVo = loginService.captchaImage();
        return RestResp.SUCCESS(captchaImageVo);
    }


    /**
     * 账号密码登录
     * @param
     * @return
     */
    @RequestMapping(value = "/account", method = RequestMethod.POST)
    public RestResp<UserInfo> loginByAccount(@RequestBody @Valid AccountLoginRequest loginRequest) {
        UserInfo userInfo = loginService.loginByAccount(loginRequest);
        return RestResp.SUCCESS(userInfo);
    }

    /**
     * 退出登陆
     * @return
     */
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public RestResp<Void> logout() {
        HeaderDTO headerDTO = getHeader();
        loginService.logout(headerDTO.getToken());
        return RestResp.SUCCESS();
    }
}
